package org.boot.myledger.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.boot.myledger.entity.User;
import org.boot.myledger.service.UserService;
import org.boot.myledger.util.ResultUtil;
import org.boot.myledger.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: liupy
 * @Date: 2024/3/28
 * 用户模块
 */
@RestController
@RequestMapping("/user")
@Api("用户相关内容模块")
@Slf4j
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public HashMap<String, Object> login(@RequestBody User user) {
        HashMap<String, Object> loginUserMap = new HashMap<>();
        String phone = user.getPhone();
        String password = user.getPassword();
//        String token = TokenUtil.sign(new User(phone, password));
        User loginUser = userService.login(phone, password);
        if (loginUser != null) {
//            loginUserMap.put("token", token);
            loginUserMap.put("user", user);
        } else {
            loginUserMap.put("token", "");
            loginUserMap.put("user", null);
        }
        return loginUserMap;
    }

    @GetMapping("/test")
    public String test() {
//        List<String> list = new ArrayList<>();
//        list.add("1");
//        list.add("2");
//        list.add("3");
//        list.add("4");
//        list.add("5");
//        String s = stringRedisTemplate.opsForValue().get("list");
//        System.out.println(s);
//        stringRedisTemplate.opsForValue().set("list",list.toString());
        return "ok";
    }


}
